package 标准接口;


import java.io.*;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;



public class test{
    public static void main(String[] args) {
        mytest("10AAEAD01AL");
    }

    public static String mytest(String value) {
        //  System.out.println("hello world");
        //方法1

        String a1="{\n" +
                "    \"digi-header\": {\n" +
                "        \"digi-key\": \"ee32dca00ae863d70957794e2db224da\",\n" +
                "        \"digi-host\": {\n" +
                "            \"prod\": \"eam\",\n" +
                "            \"ip\": \"10.20.88.30\",\n" +
                "            \"lang\": \"zh_CN\",\n" +
                "            \"timestamp\": \"2016101813254211 6\",\n" +
                "            \"acct\": \"tiptop\"\n" +
                "        },\n" +
                "        \"digi-service\": {\n" +
                "            \"prod\": \"ERP\",\n" +
                "            \"name\": \"receipt_aimm200\",\n" +
                "            \"ip\": \"192.168.0.108\",\n" +
                "            \"id\": \"topprod\"\n" +
                "        },\n" +
                "        \"digi-datakey\": {\n" +
                "            \"EntId\": \"2\",\n" +
                "            \"CompanyId\": \"ZH00\"\n" +
                "        },\n" +
                "        \"digi-protocol\": \"raw\",\n" +
                "        \"digi-type\": \"sync\"\n" +
                "    },\n" +
                "    \"digi-body\": {\n" +
                "        \"std_data\": {\n" +
                "            \"parameter\": {\n" +
                "                \"imaa001\": \""+value+"\", " +
                "                \"imaal003\": \"\", " +
                "                \"imaal004\": \"\" " +
                "              \n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}\n";
        String result= sendPost("http://192.168.0.108/wstopprd/ws/r/awsp920",a1);
        System.out.println("返回结果 result : ");
        System.out.println(result);
//解析返回结果

        JSONObject resultJson = JSONObject.parseObject(result);
        JSONObject list = resultJson.getJSONObject("digi-body");
        JSONObject lista = list.getJSONObject("std_data");
        JSONObject listb = lista.getJSONObject("execution");
        String  code = listb.getString("code");
        System.out.println();
//        System.out.println("返回结果  :"+list);
//        System.out.println("返回结果  :"+lista);
//        System.out.println("返回结果  :"+listb);
        System.out.println("返回结果code  :"+code);
        //成功的情况
        JSONArray data = lista.getJSONObject("parameter").getJSONArray("l_master_data");
        System.out.println("data:"+data);
        String jieguo="";
        if (code.equals("0")){//成功的情况
            JSONArray res=new JSONArray();  //[{}]
            for (int i=0; i<data.size(); i++) {
                //System.out.println("size:"+data.size());
                JSONObject json = data.getJSONObject(i);
                JSONObject jsonres = new JSONObject();
                String imaa001 = json.getString("imaa001");
                String imaal003 = json.getString("imaal003");
                String imaal004 = json.getString("imaal004");
                String imaa009 = json.getString("imaa009");
                String imaa006 = json.getString("imaa006");
                String imaa010 = json.getString("imaa010");
                String imaa101 = json.getString("imaa101");
                jsonres.put("imaa001",imaa001);
                jsonres.put("imaal003",imaal003);
                jsonres.put("imaal004",imaal004);
                jsonres.put("imaa009",imaa009);
                jsonres.put("imaa006",imaa006);
                jsonres.put("imaa010",imaa010);
                jsonres.put("imaa101",imaa101);
                System.out.println("返回数组："+jsonres);
                res.add(jsonres);
            }
            System.out.println("返回结果：");
            System.out.println(res);
            jieguo=res.toJSONString();
        }else{
            jieguo="查询失败！";
        }
        return jieguo;
    }

    //Post请求
    public static String sendPost(String url, String Params) {
        OutputStreamWriter out = null;
        BufferedReader reader = null;
        String response = "";
        try {
            URL httpUrl = null; //HTTP URL类 用这个类来创建连接
            //创建URL
            httpUrl = new URL(url);
            //建立连接
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("connection", "Keep-Alive");
            //    conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("version", "1.0.0");
            conn.setUseCaches(false);//设置不要缓存
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();

            //POST请求
            out = new OutputStreamWriter(
                    conn.getOutputStream(), "UTF-8");
            out.write(Params);
            out.flush();
            //读取响应
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            /* String lines;
             while ((lines = reader.readLine()) != null) {
                 lines = new String(lines.getBytes(), "UTF-8");
                 response+=lines;
             }*/
            StringBuffer sb = new StringBuffer();
            int ch;
            while ((ch = reader.read()) > -1) {
                sb.append((char) ch);
            }
            response = sb.toString();

            reader.close();
            // 断开连接
            conn.disconnect();

        } catch (Exception e) {
            // bb.writeLog("异常2222++++：" + e.getMessage());
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                // bb.writeLog("异常11122211:" + ex.getMessage());
                ex.printStackTrace();
            }
        }
        return response;
    }



    //获取http信息的方法
//    public static String httpRequest (String requestUrl, String requestMethod, String outputStr){
//        StringBuffer buffer = null;
//        try {
//            URL url = new URL(requestUrl);
//            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//            conn.setDoOutput(true);
//            conn.setDoInput(true);
//            conn.setRequestMethod(requestMethod);
//            conn.connect();
//            //往服务器端写内容 也就是发起http请求需要带的参数
//            if (null != outputStr) {
//                OutputStream os = conn.getOutputStream();
//                os.write(outputStr.getBytes("utf-8"));
//                os.close();
//            }
//
//            //读取服务器端返回的内容
//            InputStream is = conn.getInputStream();
//            InputStreamReader isr = new InputStreamReader(is, "utf-8");
//            BufferedReader br = new BufferedReader(isr);
//            buffer = new StringBuffer();
//            String line = null;
//            while ((line = br.readLine()) != null) {
//                buffer.append(line);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return buffer.toString();
//    }

}
